package com.tnovoselec.server.gdjejefrka.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;

import org.w3c.dom.Document;

import com.tnovoselec.server.gdjejefrka.beans.Location;
import com.tnovoselec.server.gdjejefrka.util.AdapterXMLUtil;
import com.tnovoselec.server.gdjejefrka.util.Util;
import com.tnovoselec.server.gdjejefrka.util.Utils;
import com.tnovoselec.server.gdjejefrka.util.XMLUtil;

@WebServlet(
        name = "GdjeJeFrkaServlet", 
        urlPatterns = {"/GdjeJeFrka/Adapter"}
    )
public class GdjeJeFrkaServlet extends HttpServlet{
	
	public static final String REPORT = "report";
	public static final String GET_REPORTS="getReports";
	public static final String LNG = "lng";
	public static final String LAT = "lat";
	public static final String UID = "uid";
	public static final String TYPE = "type";

	private static final long serialVersionUID = -4642315370555006793L;
	
	public static String XML_ENCODING = Util._encoding;
	public static String XML_VERSION = "1.0";
	
	public static Map<String, String> OUTPUT_PROPERTIES = new HashMap<String, String>(5, 1.1F);
	static {
		GdjeJeFrkaServlet.OUTPUT_PROPERTIES.put(OutputKeys.INDENT, "yes");
		GdjeJeFrkaServlet.OUTPUT_PROPERTIES.put(OutputKeys.ENCODING, GdjeJeFrkaServlet.XML_ENCODING);
		GdjeJeFrkaServlet.OUTPUT_PROPERTIES.put(OutputKeys.VERSION, GdjeJeFrkaServlet.XML_VERSION);
		GdjeJeFrkaServlet.OUTPUT_PROPERTIES.put(OutputKeys.MEDIA_TYPE, XMLUtil.XML_CONTENT_TYPE);
		GdjeJeFrkaServlet.OUTPUT_PROPERTIES.put("{http://xml.apache.org/xslt}indent-amount", "4");
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException {
		try {
			doJob(req, resp);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		try {
			doJob(req, resp);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	void doJob(HttpServletRequest req, HttpServletResponse resp) throws Exception{

		String key;
		String lng;
		String lat;
		String uid;
		String type;
		
		key = req.getParameter("key");
		boolean flag= false;
		if (key!=null && key.equals(REPORT)){
			lng = req.getParameter(LNG);
			lat = req.getParameter(LAT);
			uid = req.getParameter(UID);
			type = req.getParameter(TYPE);
			
			if (lng != null && lat!=null && uid!=null && type!=null){
				Location location = new Location();
				location.setLat(Double.parseDouble(lat));
				location.setLng(Double.parseDouble(lng));
				location.setType(Integer.parseInt(type));
				location.setUid(Long.parseLong(uid));
				location.setTimestamp(System.currentTimeMillis());
				flag = Utils.insertRowInLocations(location);
			}
			
			System.out.println(lng + ", " + lat + " , " + uid);
			resp.setStatus(flag?200:500);
			resp.getWriter().write(flag?"sve ok":"kita");
			resp.flushBuffer();
		} else if (key!=null && key.equals(GET_REPORTS)){
			lng = req.getParameter(LNG);
			lat = req.getParameter(LAT);
			uid = req.getParameter(UID);
			
			if (lng != null && lat!=null && uid!=null){
				Location location = new Location();
				location.setLat(Double.parseDouble(lat));
				location.setLng(Double.parseDouble(lng));
				location.setUid(Long.parseLong(uid));
				location.setTimestamp(System.currentTimeMillis());
				Document doc = Utils.getReports(location);
				Source source = new DOMSource(doc);
				resp.setContentType("text/xml");
				AdapterXMLUtil.serialize(source, resp.getOutputStream(), GdjeJeFrkaServlet.OUTPUT_PROPERTIES);
			}
		}
		
	}

}
